/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import com.opensymphony.xwork2.ActionContext;
import java.util.Map;
import models.AccountBO;
import myconfession.dao.AccountDAO;
import myconfession.dto.account.Account;

/**
 *
 * @author BAOLA
 */
public class RegisterAction {

    private String username;
    private String password;
    private String email;
    private String firstname;
    private String lastname;
    private String error;

    public RegisterAction() {
    }

    public String execute() throws Exception {
        // throw new UnsupportedOperationException("Not supported yet.");
        String sResult = "fail";
        Account acc = new Account();
        acc.setUsername(getUsername());
        acc.setPassword(getPassword());
        acc.setEmail(getEmail());
        acc.setFirstname(getFirstname());
        acc.setLastname(getLastname());
        if (!AccountDAO.checkExistUser(username)) {
            boolean result = AccountBO.createAccount(acc);
            if (result) {
                sResult = "success";
                Map<String, Object> session = ActionContext.getContext().getSession();
                session.put("USER", acc.getUsername());
            }
        } else {
            error = "Username already exist";
        }

        return sResult;
    }

    /**
     * @return the username
     */
    public String getUsername() {
        return username;
    }

    /**
     * @param username the username to set
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the firstname
     */
    public String getFirstname() {
        return firstname;
    }

    /**
     * @param firstname the firstname to set
     */
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    /**
     * @return the lastname
     */
    public String getLastname() {
        return lastname;
    }

    /**
     * @param lastname the lastname to set
     */
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }
}
